package com.zjeav.lingjiao.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.response.SubjectResponse;

import java.util.ArrayList;

public class HorizontalAdapter extends BaseAdapter {

    private ArrayList<SubjectResponse> subjects;
    private Context mContext;

    public HorizontalAdapter(ArrayList<SubjectResponse> subjects, Context mContext) {
        super();
        this.subjects = subjects;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {

        if (subjects != null) {

            return subjects.size();
        } else {

            return 0;
        }
    }

    @Override
    public Object getItem(int position) {

        if (subjects != null) {

            return subjects.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_rec_user, null);
            holder.item_avatar = (ImageView) convertView.findViewById(R.id.avatar);
            holder.item_name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (subjects != null && subjects.get(position) != null) {

            Glide.with(mContext)
                    .load(subjects.get(position).getIcon())
                    .into(holder.item_avatar);
            holder.item_name.setText(subjects.get(position).getName());
        }
        return convertView;
    }

    class ViewHolder {
        ImageView item_avatar;
        TextView item_name;
    }
}